// File: fms.go
package main

import (
	"fmt"
	"io"
	"log"
	"os"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/aws/aws-sdk-go/service/s3/s3iface"
	"github.com/suyuan32/simple-admin-file/pkg/upload"
	"github.com/suyuan32/simple-admin-file/pkg/upload/cloudfile"
	"mime/multipart"
	"net/http"
	"net/textproto"
)

// MockS3Client 模拟 S3 客户端
type MockS3Client struct {
	s3iface.S3API
	uploadFunc func(input *s3.PutObjectInput) (*s3.PutObjectOutput, error)
}

func (m *MockS3Client) PutObjectWithContext(ctx context.Context, input *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
	return m.uploadFunc(input)
}

// setupUploaderForTest 构建一个用于测试的 CloudUploader 实例
func setupUploaderForTest() *cloudfile.CloudUploader {
	mockStorage := map[string]upload.StorageClient{
		"aws": &cloudfile.S3Adapter{
			Client: &MockS3Client{
				uploadFunc: func(input *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
					log.Printf("Mock upload to bucket %s, key %s", *input.Bucket, *input.Key)
					return &s3.PutObjectOutput{}, nil
				},
			},
		},
		"tencent": &cloudfile.S3Adapter{
			Client: &MockS3Client{
				uploadFunc: func(input *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
					log.Printf("Mock upload to Tencent bucket %s, key %s", *input.Bucket, *input.Key)
					return &s3.PutObjectOutput{}, nil
				},
			},
		},
		"aliyun": &cloudfile.S3Adapter{
			Client: &MockS3Client{
				uploadFunc: func(input *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
					log.Printf("Mock upload to Aliyun bucket %s, key %s", *input.Bucket, *input.Key)
					return &s3.PutObjectOutput{}, nil
				},
			},
		},
	}

	cfg := &upload.Config{
		MaxImageSize:        10 << 20,  // 10MB
		MaxVideoSize:        100 << 20, // 100MB
		MaxAudioSize:        30 << 20,  // 30MB
		MaxOtherSize:        1 << 20,   // 1MB
		DefaultProvider:     "aws",
		PrivateStorePath:    "private",
		PublicStorePath:     "public",
		ServerURL:           "http://localhost:8080",
		DeleteFileWithCloud: true,
	}

	providerData := map[string]upload.ProviderData{
		"aws": {
			Bucket:   "my-bucket",
			Endpoint: "s3.amazonaws.com",
			Folder:   "uploads",
			UseCdn:   true,
			CdnUrl:   "https://cdn.aws.com/",
			Id:       1,
		},
		"tencent": {
			Bucket:   "tencent-bucket",
			Endpoint: "cos.ap-guangzhou.myqcloud.com",
			Folder:   "uploads",
			UseCdn:   true,
			CdnUrl:   "https://cdn.tencent.com/",
			Id:       2,
		},
		"aliyun": {
			Bucket:   "aliyun-bucket",
			Endpoint: "oss-cn-beijing.aliyuncs.com",
			Folder:   "uploads",
			UseCdn:   true,
			CdnUrl:   "https://cdn.aliyun.com/",
			Id:       3,
		},
	}

	return &cloudfile.CloudUploader{
		config:   cfg,
		storage:  mockStorage,
		provider: providerData,
	}
}

func main() {
	uploader := setupUploaderForTest()

	// 打开文件
	file, err := os.Open("example.jpg")
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer file.Close()

	// 创建文件头
	handler := &multipart.FileHeader{
		Filename: "example.jpg",
		Size:     1024,
		Header:   textproto.MIMEHeader(http.Header{}),
	}
	handler.Header.Set("Content-Type", "image/jpeg")

	// 上传文件
	result, err := uploader.Upload(file, handler, "aws")
	if err != nil {
		fmt.Println("Error uploading file:", err)
		return
	}

	fmt.Printf("Upload successful: %+v\n", result)
}
